package com.topnav.mc.task;

import com.js.lib.result.Result;
import com.topnav.mc.admin.entity.GbServer;
import com.topnav.mc.admin.entity.bean.GbServerBean;
import com.topnav.mc.gbserverapi.DeviceApi;
import com.topnav.mc.gbserverapi.bean.GbDevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.Future;

/**
 * @Author: kenny
 * @Date: 2023/10/26 15:58
 */
@Slf4j
@Component
public class DeviceUpdateToGbServerTask extends AbstractTask<GbDevice> {
    private String host = "http://%s:%s";
    private String url=null;
    @Resource
    private DeviceApi deviceApi;

/*    @Qualifier("taskExecutor")
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;*/


    @Override
    public Future<String> execute(GbDevice device) throws Exception {
        return null;
    }

    @Override
    public Future<GbDevice> execute1(GbDevice device) throws Exception {
        return null;
    }

    public Future<Result> updateToGbSvr(GbDevice device, GbServer gbServer) throws Exception {
        url = String.format(host,gbServer.getHost(),gbServer.getApiPort());
        return this.execute2(device);
    }
    @Async("taskExecutor")
    @Override
    public Future<Result> execute2(GbDevice gbDevice) throws Exception {

/*        taskExecutor.execute(() -> {

        });*/

        Result result =  deviceApi.updateDevice(url,gbDevice);
        return  new AsyncResult<Result>(result);
    }
}
